/**
 * ...
 * @author Franco Ponticelli
 */

package uform.property;

class LimitedFloatPropertyDecorator extends PropertyDecorator<Null<Float>>
{
	public var max(default, setMax) : Null<Float>;
	public var min(default, setMin) : Null<Float>;
	public var maxInclusive(default, setMaxInclusive) : Null<Bool>;
	public var minInclusive(default, setMinInclusive) : Null<Bool>;
	
	public function new(decorated : IProperty<Float>)
	{
		super(decorated);
		minInclusive = true;
		maxInclusive = true;
	}
	
	function setMax(value : Null<Float>)
	{
		max = value;
		set(get());
		return max;
	}
	
	function setMin(value : Null<Float>)
	{
		min = value;
		set(get());
		return min;
	}
	
	function setMaxInclusive(value : Bool)
	{
		maxInclusive = value;
		set(get());
		return maxInclusive;
	}
	
	function setMinInclusive(value : Bool)
	{
		minInclusive = value;
		set(get());
		return minInclusive;
	}
	
	override function set(value : Null<Float>) : IProperty<Null<Float>>
	{
		if (null != value)
		{
			if (max != null && ((maxInclusive && value >= max) || (!maxInclusive && value > max)))
			{
				value = max;
			} else if (min != null && ((minInclusive && value <= min) || (!minInclusive && value < min)))
			{
				value = min;
			}
		}
		decorated.set(value);
		return this;
	}
}